package groups;

import beans.SessionManagedBean;
import com.icesoft.faces.async.render.SessionRenderer;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import lecture.Lecture;
import lecture.LectureManagedBean;
import user.User;

/**
 *
 * @author Thorsten Ludwig
 */
@ManagedBean(name="accountingBean")
@RequestScoped
public class AccountingManagedBean {
    
    /**
     * set if the user wants to take part at the exercise groups
     */
    private boolean takePartExerciseGroups;
    /**
     * the choosen exercise groups
     */
    private String[] options;
    /**
     * number of exercise groups the user has to choose
     */
    private int numOptions;

    
    public AccountingManagedBean() {
        this.takePartExerciseGroups = false;
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        
        this.setNumOptions(lecture.getInfo().getHowManyExceriseGroups());
    }
    
    /**
     * return the choosen exercise groups
     * @return names of the choosen exercise groups
     */
    public String[] getOptions() {
        return options;
    }

    /**
     * set the choosen exercise groups
     * @param options choosen options
     */
    public void setOptions(String[] options) {
        this.options = options;
    }

    /**
     * get the number of options the user has to choose
     * @return 
     */
    public int getNumOptions() {
        return numOptions;
    }

    /**
     * set the number of options the user has to choose
     * @param numOptions new number
     */
    public void setNumOptions(int numOptions) {
        this.numOptions = numOptions;
        this.options = new String[numOptions];
    }
            
    /**
     * return avaible exercise groups
     * @return avaible exercise Groups
     */
    public List<String> getGroupsForSelection(int end) {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        
        List<String> list = new ArrayList<String>();
        if (lecture.getInfo().isRegisterByTime())  {
            for (MeetingDate e: lecture.getTimes()) {           
                list.add(e.toString());
            }
        } else {
                
            for (ExerciseGroup e: lecture.getInfo().getExerciseGroups()) {           
                list.add(e.getName());
            }
        }
        for (int i =0;i<end;i++) {
            System.out.append("Entferne an Stelle " +end + " Option: "+ this.options[i]);
            list.remove(this.options[i]);
        }
        
        return list;
    }

    public boolean isTakePartExerciseGroups() {
        return takePartExerciseGroups;
    }

    public void setTakePartExerciseGroups(boolean takePartExerciseGroups) {
        this.takePartExerciseGroups = takePartExerciseGroups;
        SessionRenderer.render("all");
        
    }
    
    /**
     * Add the wishes of the user
     * @return URL where the user is directet to
     */
    public String doAccounting() {
        System.out.println("doBelegen was called");
        
        FacesContext context = FacesContext.getCurrentInstance();        
        SessionManagedBean sb = (SessionManagedBean) context.getApplication().evaluateExpressionGet(context, "#{sessionBean}", SessionManagedBean.class);
        Lecture lecture = sb.getLectureBean().getLecture();
        User user = sb.getUser();
        lecture.addUser(user);    
        if (this.takePartExerciseGroups) 
        {
            lecture.addWish(user.getUsername(),this.options);
        }
        return "showWishes.jsf";
    }
}
